﻿using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

namespace PLCWpfTest.Converters
{
    internal class IntToSolidColorBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                throw new ArgumentNullException("Value can not be null");
            }
            Color color;
            switch ((int)value)
            {
                case 0:
                default:
                    color = Colors.White;
                    break;
                case 1:
                    color = Colors.Yellow;
                    break;
                case 2:
                    color = Colors.Red;
                    break;
            }
            SolidColorBrush brush = new SolidColorBrush(color);
            return brush;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                throw new ArgumentNullException("Value can not be null");
            }
            Color color = ((SolidColorBrush)value).Color;
            int type;
            if (color == Colors.Yellow)
            {
                type = 2;
            }
            else if (color == Colors.Red)
            {
                type = 3;
            }
            else
            {
                type = 1;
            }
            return type;
        }
    }
}
